<?php
#广告账户余额月度结算
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bizAd\AdWallet as mAdWallet;
use Biz\model\bizAd\AdWalletLog as mAdWalletLog;
use Biz\model\bizAd\AdWalletMonthSettle as mAdWalletMonthSettle;

function scanWallets()
{
    $currentMonth['month'] = date('Ym', time());

    $currentMonth['begin_time'] = date('Y-m-01 00:00:00', time());

    $currentMonth['end_time'] = date('Y-m-t 23:59:59', time());

    $lastMonth['begin_time'] = date('Y-m-01 00:00:00', strtotime('-1 month'));

    $lastMonth['end_time'] = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));

    $lastMonth['month'] = date('Ym', strtotime($lastMonth['begin_time']));

    mAdWallet::dataChunk(['data_status' => mAdWallet::STATUS_NORMAL, 'create_time[<=]' => $lastMonth['end_time']], 100,
        function ($data) use ($currentMonth, $lastMonth) {

            if (empty($data)) {
                return false;
            }

            $uids = array_column($data, 'uid');

            $lastMonthSettles = mAdWalletMonthSettle::dataSelect(['month' => $lastMonth['month'], 'uid' => $uids]);

            $lastMonthSettleMap = empty($lastMonthSettles) ? [] : array_column($lastMonthSettles, null, 'uid');

            foreach ($data as $row) {

                if (isset($lastMonthSettleMap[$row['uid']])) {
                    continue;
                }

                //本月第一次操作记录
                $firstLog = mAdWalletLog::dataSelectInfo(
                    [
                        'uid' => $row['uid'],
                        'create_time[>=]' => $currentMonth['begin_time'],
                        'create_time[<=]' => $currentMonth['end_time'],
                        'ORDER' => ['version' => 'ASC']
                    ]
                );

                //上月末余额
                $lastBalance = empty($firstLog) ? $row['balance'] : $firstLog['old_balance'];

                //在途金额
                $lockAmount = empty($firstLog) ? $row['lock_amount'] : $firstLog['old_lock_amount'];

                //上月充值金额
                $lastMonthRechargeAmount = mAdWalletLog::sum('amount',
                    [
                        'uid' => $row['uid'],
                        'op_type' => mAdWalletLog::OP_TYPE_RECHARGE,
                        'create_time[>=]' => $lastMonth['begin_time'],
                        'create_time[<=]' => $lastMonth['end_time'],
                    ]
                );

                $lastMonthRechargeAmount = $lastMonthRechargeAmount ?: 0.00;

                //上月计划扣除金额
                $lastMonthCostAmount = mAdWalletLog::sum('amount',
                    [
                        'uid' => $row['uid'],
                        'op_type' => [mAdWalletLog::OP_TYPE_CONSUME],
                        'create_time[>=]' => $lastMonth['begin_time'],
                        'create_time[<=]' => $lastMonth['end_time'],
                    ]
                );

                $lastMonthCostAmount = $lastMonthCostAmount ?: 0.00;

                //上月计划返还金额
                $lastMonthRefundAmount = mAdWalletLog::sum('amount',
                    [
                        'uid' => $row['uid'],
                        'op_type' => [mAdWalletLog::OP_TYPE_REFUND],
                        'create_time[>=]' => $lastMonth['begin_time'],
                        'create_time[<=]' => $lastMonth['end_time'],
                    ]
                );

                $lastMonthRefundAmount = $lastMonthRefundAmount ?: 0.00;

                //上月提现金额
                $lastMonthCashOutAmount = mAdWalletLog::sum('amount',
                    [
                        'uid' => $row['uid'],
                        'op_type' => [mAdWalletLog::OP_TYPE_CASHOUT],
                        'create_time[>=]' => $lastMonth['begin_time'],
                        'create_time[<=]' => $lastMonth['end_time'],
                    ]
                );

                $lastMonthCashOutAmount = $lastMonthCashOutAmount ?: 0.00;

                $insertData = [
                    'uid' => $row['uid'],
                    'utype' => $row['utype'],
                    'month' => $lastMonth['month'],
                    'balance' => $lastBalance,
                    'recharge_amount' => $lastMonthRechargeAmount,
                    'cost_amount' => $lastMonthCostAmount,
                    'refund_amount' => $lastMonthRefundAmount,
                    'cashout_amount' => $lastMonthCashOutAmount,
                    'lock_amount' => $lockAmount,
                    'create_time' => date('Y-m-d H:i:s'),
                ];

                mAdWalletMonthSettle::dataInsert($insertData);
            }

            return true;
        }
    );

}

scanWallets();